﻿using Com.JunCaiSoft.Accountant.Common;
using Com.JunCaiSoft.AutoMapper;
using Com.JunCaiSoft.Common.AssistClass;
using Com.JunCaiSoft.ConstructionCashFlow.Common.Enum;
using Com.JunCaiSoft.ConstructionCashFlow.Dto;
using Com.JunCaiSoft.ConstructionCashFlow.Entity;
using Com.JunCaiSoft.ConstructionCashFlow.Model;
using Com.JunCaiSoft.Freeze.Entity;
using Com.JunCaiSoft.Freeze.Model;
using Microsoft.AspNetCore.Builder;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Com.JunCaiSoft.ConstructionCashFlow
{
    public static class ConstructionCashFlowAutoMapperConfigure
    {
        public static void CreateConstructionCashFlowMapper(IApplicationBuilder app)
        {
            var creator = app.UseAutoMapper();

            #region 示例
            //creator.CreateMap<User, LoginUserInfo>()
            //    .ForMember(dest => dest.uid, opt => opt.MapFrom(src => src.Id))
            //    .ForMember(dest => dest.nickname, opt => opt.MapFrom(src => src.NickName));

            //creator.CreateMap<Authorize, AuthorizeTreeNode>()
            //    .ForMember(dest => dest.pId, opt => opt.MapFrom(src => src.PID))
            //    .ForMember(dest => dest.id, opt => opt.MapFrom(src => src.ID))
            //    .ForMember(dest => dest.claimType, opt => opt.MapFrom(src => src.ControllerName))
            //    .ForMember(dest => dest.claimValue, opt => opt.MapFrom(src => src.ActionName))
            //    .ForMember(dest => dest.expanded, opt => opt.MapFrom(src => true) )
            //    .ForMember(dest => dest.isParent, opt => opt.MapFrom(src => false));
            #endregion

            #region 银行实体映射
            creator.CreateMap<BankView, Bank>()
                .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id))
                .ForMember(dest => dest.Num, opt => opt.MapFrom(src => src.Num))
                .ForMember(dest => dest.NumCode, opt => opt.MapFrom(src => src.NumCode))
                .ForMember(dest => dest.Province, opt => opt.MapFrom(src => src.Province))
                .ForMember(dest => dest.City, opt => opt.MapFrom(src => src.City))
                .ForMember(dest => dest.Zone, opt => opt.MapFrom(src => src.Zone))
                .ForMember(dest => dest.Company, opt => opt.MapFrom(src => src.Company))
                .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Name))
                .ForMember(dest => dest.Account, opt => opt.MapFrom(src => src.Account))
                .ForMember(dest => dest.AccountType, opt => opt.MapFrom(src => src.AccountType))
                .ForMember(dest => dest.IsUsed, opt => opt.MapFrom(src => src.IsUsed))
                .ForMember(dest => dest.CreateTime, opt => opt.MapFrom(src => src.CreateTime));
            creator.CreateMap<BankView, BankSettlement>()
                .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.SettlementBankId))
                .ForMember(dest => dest.Num, opt => opt.MapFrom(src => src.Num))
                .ForMember(dest => dest.NumCode, opt => opt.MapFrom(src => src.SettlementBankId == Guid.Empty ? TimeStamp.timestamp.ToString() : src.NumCode))
                .ForMember(dest => dest.SettlementBankId, opt => opt.MapFrom(src => src.Id))
                .ForMember(dest => dest.BeginTime, opt => opt.MapFrom(src => src.SettlementBankId == Guid.Empty ? Convert.ToDateTime(DateTimeExtension.GetCurrentMonthFirstDay()) : src.BeginTime))
                .ForMember(dest => dest.EndTime, opt => opt.MapFrom(src => src.SettlementBankId == Guid.Empty ? Convert.ToDateTime(DateTimeExtension.GetCurrentMonthLastDay()) : src.EndTime))
                .ForMember(dest => dest.SettlementPeriod, opt => opt.MapFrom(src => src.SettlementBankId == Guid.Empty ? SettlementPeriod.BEGIN : src.SettlementPeriod))
                .ForMember(dest => dest.BeginBalance, opt => opt.MapFrom(src => src.BeginBalance))
                .ForMember(dest => dest.EndBalance, opt => opt.MapFrom(src => src.BeginBalance))
                .ForMember(dest => dest.BeginCommercialTicket, opt => opt.MapFrom(src => src.BeginCommercialTicket))
                .ForMember(dest => dest.EndCommercialTicket, opt => opt.MapFrom(src => src.EndCommercialTicket))
                .ForMember(dest => dest.BeginFreezeMoney, opt => opt.MapFrom(src => src.FreezeMoney))
                .ForMember(dest => dest.EndFreezeMoney, opt => opt.MapFrom(src => src.FreezeMoney))
                .ForMember(dest => dest.BeginFlowBalance, opt => opt.MapFrom(src => src.FlowBalance))
                .ForMember(dest => dest.EndFlowBalance, opt => opt.MapFrom(src => src.FlowBalance))
                .ForMember(dest => dest.IsSettlement, opt => opt.MapFrom(src => src.SettlementBankId == Guid.Empty ? false : src.IsSettlement))
                .ForMember(dest => dest.CreateTime, opt => opt.MapFrom(src => src.SettlementBankId == Guid.Empty ? DateTime.Now : src.CreateTime));
            #endregion

            #region 项目实体映射
            creator.CreateMap<ProjectView, Project>();
            creator.CreateMap<ProjectView, ProjectSettlement>()
                .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.SettlementProjectId))
                .ForMember(dest => dest.Num, opt => opt.MapFrom(src => src.Num))
                .ForMember(dest => dest.NumCode, opt => opt.MapFrom(src => src.SettlementProjectId == Guid.Empty ? TimeStamp.timestamp.ToString() : src.NumCode))
                .ForMember(dest => dest.SettlementProjectId, opt => opt.MapFrom(src => src.Id))
                .ForMember(dest => dest.BeginTime, opt => opt.MapFrom(src => src.SettlementProjectId == Guid.Empty ? Convert.ToDateTime(DateTimeExtension.GetCurrentMonthFirstDay()) : src.BeginTime))
                .ForMember(dest => dest.EndTime, opt => opt.MapFrom(src => src.SettlementProjectId == Guid.Empty ? Convert.ToDateTime(DateTimeExtension.GetCurrentMonthFirstDay()) : src.EndTime))
                .ForMember(dest => dest.SettlementPeriod, opt => opt.MapFrom(src => src.SettlementProjectId == Guid.Empty ? SettlementPeriod.BEGIN : src.SettlementPeriod))
                .ForMember(dest => dest.BeginBalance, opt => opt.MapFrom(src => src.BeginBalance))
                .ForMember(dest => dest.EndBalance, opt => opt.MapFrom(src => src.BeginBalance))
                .ForMember(dest => dest.BeginFreezeMoney, opt => opt.MapFrom(src => src.FreezeMoney))
                .ForMember(dest => dest.EndFreezeMoney, opt => opt.MapFrom(src => src.FreezeMoney))
                .ForMember(dest => dest.BeginFlowBalance, opt => opt.MapFrom(src => src.FlowBalance))
                .ForMember(dest => dest.EndFlowBalance, opt => opt.MapFrom(src => src.FlowBalance))
                .ForMember(dest => dest.BeginCashBalance, opt => opt.MapFrom(src => src.CashBalance))
                .ForMember(dest => dest.EndCashBalance, opt => opt.MapFrom(src => src.CashBalance))
                .ForMember(dest => dest.BeginCommercialTicket, opt => opt.MapFrom(src => src.CommercialTicket))
                .ForMember(dest => dest.EndCommercialTicket, opt => opt.MapFrom(src => src.CommercialTicket))
                .ForMember(dest => dest.BeginGongDiFang, opt => opt.MapFrom(src => src.GongDiFang))
                .ForMember(dest => dest.EndGongDiFang, opt => opt.MapFrom(src => src.GongDiFang))
                .ForMember(dest => dest.IsSettlement, opt => opt.MapFrom(src => src.SettlementProjectId == Guid.Empty ? false : src.IsSettlement))
                .ForMember(dest => dest.CreateTime, opt => opt.MapFrom(src => src.SettlementProjectId == Guid.Empty ? DateTime.Now : src.CreateTime));
            #endregion

            #region 现金台账实体映射
            creator.CreateMap<CashFlowView, CashFlow>()
                .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id))
                .ForMember(dest => dest.Num, opt => opt.MapFrom(src => src.Num))
                .ForMember(dest => dest.NumCode, opt => opt.MapFrom(src => src.NumCode))
                .ForMember(dest => dest.BankId, opt => opt.MapFrom(src => src.BankGuid))
                .ForMember(dest => dest.BankForward, opt => opt.MapFrom(src => (BookMark)Enum.Parse(typeof(BookMark), src.MoneyForward) == BookMark.Credit ? BookMark.Credit : BookMark.Debit))
                .ForMember(dest => dest.ProjectId, opt => opt.MapFrom(src => src.ProjectGuid))
                .ForMember(dest => dest.ProjectForward, opt => opt.MapFrom(src => (BookMark)Enum.Parse(typeof(BookMark), src.MoneyForward) == BookMark.Credit ? BookMark.Debit : BookMark.Credit))
                .ForMember(dest => dest.Summary, opt => opt.MapFrom(src => src.Summary))
                .ForMember(dest => dest.FundsType, opt => opt.MapFrom(src => src.FundsType))
                .ForMember(dest => dest.Amount, opt => opt.MapFrom(src => src.Amount))
                .ForMember(dest => dest.FundsStatus, opt => opt.MapFrom(src => src.FundsStatus))
                .ForMember(dest => dest.PayOperatorId, opt => opt.MapFrom(src => src.PayOperatorId))
                .ForMember(dest => dest.AllocationTime, opt => opt.MapFrom(src => src.AllocationTime))
                .ForMember(dest => dest.CreateTime, opt => opt.MapFrom(src => src.CreateTime));
            creator.CreateMap<CashFlow, CashFlowView>()
                .ForMember(dest => dest.MoneyForward, opt => opt.MapFrom(src => Convert.ToInt32(src.BankForward).ToString()))
                .ForMember(dest => dest.BankGuid, opt => opt.MapFrom(src => src.BankId))
                .ForMember(dest => dest.ProjectGuid, opt => opt.MapFrom(src => src.ProjectId));
            #endregion


        }
    }
}
